﻿using System.IO;
using System.Windows.Forms;
using System;
using System.Data.SQLite;

namespace MemprizeVocabulary
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            if (!File.Exists("./database.sqlite3"))
            {
                CheckRslt.Text = "词库为空！";
                //做一些处理填充词库。
            }

            func();
        }

        Database databaseObject = new Database();
        String query = "SELECT * FROM Word ORDER BY random() LIMIT 1";
        String correctAnswer;
        String answer="";
        String pron;

        public void func()
        { 
            SQLiteCommand myCommand = new SQLiteCommand(query, databaseObject.myConnection);
            databaseObject.OpenConnection();
            SQLiteDataReader result = myCommand.ExecuteReader();

            if (result.HasRows)
            {
                while (result.Read())
                {
                    tbxExp.Text = result["chineseExp"].ToString();
                    pron = result["pron"].ToString();
                    correctAnswer = result["englishWord"].ToString();
                }
            }
        }
        
        private void Check(object sender, EventArgs e)
        {
            if (answer == correctAnswer)
            {
                CheckRslt.Text = "Correct!";
            }
            else
            {
                CheckRslt.Text = "Wrong!";
            }
        }
        private void tbxWord_TextChanged(object sender, EventArgs e)
        {
            try
            {
                answer = tbxWord.Text;
            }
            catch { Console.WriteLine("nothing."); }

        }

        private void btCLue_Click(object sender, EventArgs e)
        {
            tbxPron.Text = pron;
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            func();
            tbxPron.Text = "";
            CheckRslt.Text = "";
            tbxWord.Text = "";

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            databaseObject.CloseConnection();
        }
    }
}
